package com.easyshopping;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

import com.easyshopping.domain.Currency;
import com.easyshopping.domain.Item;
import com.easyshopping.domain.ItemCategory;
import com.easyshopping.domain.Unit;
import com.easyshopping.facade.ShoppingListFacade;

public class ItemInfoActivity extends Activity implements OnClickListener,
		OnItemSelectedListener {

	private Button btn_OK;
	private Button btn_Back;
	private EditText et_name;
	private EditText et_amount;
	private EditText et_price;
	// private EditText et_Category;
	private Spinner sp_Category;
	private Spinner sp_Currency;
	private Spinner sp_unit;

	private Item item;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.info);

		Bundle extras = getIntent().getExtras();
		int item_id = ((Integer) extras.get(Item.ITEM_ID)).intValue();

		item = ShoppingListFacade.getInstance().fetchItem(item_id);

		btn_OK = (Button) this.findViewById(R.id.Btn_OK);
		btn_Back = (Button) this.findViewById(R.id.Btn_Back);
		et_name = (EditText) this.findViewById(R.id.Edit_Item_Name);
		et_amount = (EditText) this.findViewById(R.id.Edit_Item_Amount);
		et_price = (EditText) this.findViewById(R.id.Edit_Item_Price);
		
		//test by xiaoyu
		// et_Category = (EditText) this.findViewById(R.id.ET_Category);
		// et_Category.setOnClickListener(this);

		sp_Category = (Spinner) this.findViewById(R.id.Sp_Category);
		sp_Category.setOnItemSelectedListener(this);
		Cursor cur = ShoppingListFacade.getInstance().getPersistence()
				.fetchItemCategories();

		String[] from = new String[] { ItemCategory.ITEMCATEGORY_NAME };
		int[] to = new int[] { android.R.id.text1 };
		SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
				android.R.layout.simple_spinner_item, cur, from, to);
		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_Category.setAdapter(adapter2);

//		set the right category for the item
		for (int i = 0; i < sp_Category.getCount(); i++) {
			
			Cursor c = (Cursor)sp_Category.getItemAtPosition(i);
			int id = c.getInt(c.getColumnIndex(ItemCategory.ITEMCATEGORY_ID));
			if (id == item.getCat_id()) {
				sp_Category.setSelection(i);
				break;
			}
		}
		
		sp_Currency = (Spinner) this.findViewById(R.id.Sp_currency);
		sp_Currency.setOnItemSelectedListener(this);
		fillCurrency();
		
		sp_unit = (Spinner)this.findViewById(R.id.Sp_unit);
		sp_unit.setOnItemSelectedListener(this);
		fillUnit();
		
//		ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
//				android.R.layout.simple_spinner_item, items);
//		aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//		sp_Currency.setAdapter(aa);
		//end test
		
		et_name.setText(item.getName());
		if (item.getAmount() != 0)
			et_amount.setText(new Double(item.getAmount()).toString());
		if (item.getPrice() != 0)
			et_price.setText(new Double(item.getPrice()).toString());

		btn_OK.setOnClickListener(this);
		btn_Back.setOnClickListener(this);
	}

	private void fillUnit() {
		
		Cursor cursor = ShoppingListFacade.getInstance().getPersistence().fetchUnits();
		String[] from = new String[] { Unit.UNIT_NAME };
		int[] to = new int[] { android.R.id.text1 };
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_spinner_item, cursor, from, to);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_unit.setAdapter(adapter);
		
		for (int i = 0; i < sp_unit.getCount(); i++) {
			
			Cursor c = (Cursor)sp_unit.getItemAtPosition(i);
			int id = c.getInt(c.getColumnIndex(Unit.UNIT_ID));
			if (id == item.getUnit_id()) {
				sp_unit.setSelection(i);
				break;
			}
		}
	}

	private void fillCurrency() {
		
		Cursor cursor = ShoppingListFacade.getInstance().getPersistence().fetchCurrencies();
		String[] from = new String[] { Currency.CURRENCY_NAME };
		int[] to = new int[] { android.R.id.text1 };
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_spinner_item, cursor, from, to);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_Currency.setAdapter(adapter);
		
		for (int i = 0; i < sp_Currency.getCount(); i++) {
			
			Cursor c = (Cursor)sp_Currency.getItemAtPosition(i);
			int id = c.getInt(c.getColumnIndex(Currency.CURRENCY_ID));
			if (id == item.getCur_id()) {
				sp_Currency.setSelection(i);
				break;
			}
		}
	}

	public void onClick(View v) {

		if (v.getId() == R.id.Btn_OK) {

			if (et_name.getText().toString().trim().length() == 0){
				et_name.setHint(R.string.cantbeempty);
				return;
			}
			
			item.setName(et_name.getText().toString().trim());
			if (et_amount.getText().toString().trim().length() != 0) {
				item.setAmount(Double.parseDouble(et_amount.getText()
						.toString()));
			}
			if (et_price.getText().toString().trim().length() != 0) {
				item.setPrice(Double.parseDouble(et_price.getText()
								.toString()));
			}

			ShoppingListFacade.getInstance().updateItem(item);
			this.finish();
			return;
		}

		if (v.getId() == R.id.Btn_Back) {
			this.finish();
			return;
		}

		// if (v.getId() == R.id.ET_Category) {
		// Intent intent = new Intent(this, SelectCategoryActivity.class);
		// this.startActivity(intent);
		// return;
		// }
	}

	public void onItemSelected(AdapterView<?> l, View view, int position,
			long id) {
		
		Log.i("ItemInfoActivity", "item selected");
		
		if (l.getId() == R.id.Sp_Category) {
			Cursor c = (Cursor)l.getItemAtPosition(position);
			int cat_id = c.getInt(c.getColumnIndex(ItemCategory.ITEMCATEGORY_ID));
			item.setCat_id(cat_id);
			return;
		}
		
		if (l.getId() == R.id.Sp_currency) {
			Cursor c = (Cursor)l.getItemAtPosition(position);
			int cur_id = c.getInt(c.getColumnIndex(Currency.CURRENCY_ID));
			item.setCur_id(cur_id);
			return;
		}
		
		if (l.getId() == R.id.Sp_unit) {
			Cursor c = (Cursor)l.getItemAtPosition(position);
			int unit_id = c.getInt(c.getColumnIndex(Unit.UNIT_ID));
			item.setUnit_id(unit_id);
			return;
		}
		
	}

	public void onNothingSelected(AdapterView<?> arg0) {

	}

}
